from mainoption import mainOption
from history import dict_history
from common.iniconfig import iniconfig
import wx
import lang
import gettext

class dict_option(mainOption):
	auto_read=iniconfig("settings")
        def __init__(self,parent):
                mainOption.__init__(self,parent)
                self.Bind( wx.EVT_INIT_DIALOG, self.OnInitDialog )
		self.m_Ok.Bind( wx.EVT_BUTTON, self.OnOk )
		self.m_Cancel.Bind( wx.EVT_BUTTON, self.OnCancel )
		self.m_Apply.Bind( wx.EVT_BUTTON, self.OnApply )
		self.m_Ok.SetDefault()
		
        
	def OnInitDialog( self, event ):
		self.history=dict_history()
		self.m_history.SetValue(str(self.history.get_num()))
		self.a=lang.getlang()
		for m in self.a:
			self.m_blang.Append(m + _(" from ") + self.a[m],wx.Bitmap( u"locale/%s/LC_MESSAGES/lang_ex.jpg" % m, wx.BITMAP_TYPE_ANY ))
		self.formlang=lang.get_default_lang()
		#self.m_blang.SetValue(lang.get_default_lang() + _(" from ") + self.a[lang.get_default_lang()])
		temp=-1
		for m in self.a:
			temp=temp+1
			if m==self.formlang:
				self.m_blang.SetSelection(temp)
				break
		
		if self.auto_read.get_item("auto_read")=="True":
		        self.m_choice1.SetSelection(0)
		else:
			self.m_choice1.SetSelection(1)	
		
	def OnOk( self, event ):
		if self.m_choice1.GetSelection()==0:
			self.auto_read.set_item("auto_read","True")
		else:
			self.auto_read.set_item("auto_read","False")
		self.history.set_num(self.m_history.GetValue())
		temp=self.m_blang.GetValue()
		temp2=temp.find(_(" from "))
		lang.set_default_lang(temp[:temp2])
		if temp[:temp2]!=self.formlang:
			gettext.translation('lang', './locale', languages=[lang.get_default_lang()]).install(True)
			wx.MessageBox(_("Your language setting comes into force as soon as the program restarts."),
		              _("settings"),wx.OK | wx.ICON_INFORMATION,self)
		self.Close()
			
	def OnCancel( self, event ):
		self.Close()
			
	def OnApply( self, event ):
		if self.m_choice1.GetSelection()==0:
			self.auto_read.set_item("auto_read","True")
		else:
			self.auto_read.set_item("auto_read","False")
                self.history.set_num(self.m_history.GetValue())
		temp=self.m_blang.GetValue()
		temp2=temp.find(_(" from "))
		lang.set_default_lang(temp[:temp2])		